package com.bcmusic.user.controller;

import com.bcmusic.user.config.TokenDecode;
import com.bcmusic.user.pojo.EnterpriseIdentification;
import com.bcmusic.user.pojo.MusicianIdentification;
import com.bcmusic.user.pojo.User;
import com.bcmusic.user.service.EnterpriseIdentificationService;
import com.bcmusic.user.service.UserService;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/enterpriseIdentification")
@CrossOrigin
public class EnterpriseIdentificationController {

    @Autowired
    private EnterpriseIdentificationService enterpriseIdentificationService;

    @Autowired
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TokenDecode tokenDecode;

    /***
     * EnterpriseIdentification分页条件搜索实现
     * @param enterpriseIdentification
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) EnterpriseIdentification enterpriseIdentification, @PathVariable int page, @PathVariable int size) {
        //调用EnterpriseIdentificationService实现分页条件查询EnterpriseIdentification
        PageInfo<EnterpriseIdentification> pageInfo = enterpriseIdentificationService.findPage(enterpriseIdentification, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * EnterpriseIdentification分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用EnterpriseIdentificationService实现分页查询EnterpriseIdentification
        PageInfo<EnterpriseIdentification> pageInfo = enterpriseIdentificationService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param enterpriseIdentification
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<EnterpriseIdentification>> findList(@RequestBody(required = false) EnterpriseIdentification enterpriseIdentification) {
        //调用EnterpriseIdentificationService实现条件查询EnterpriseIdentification
        List<EnterpriseIdentification> list = enterpriseIdentificationService.findList(enterpriseIdentification);
        return new Result<List<EnterpriseIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用EnterpriseIdentificationService实现根据主键删除
        enterpriseIdentificationService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改EnterpriseIdentification数据
     * @param enterpriseIdentification
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody EnterpriseIdentification enterpriseIdentification, @PathVariable Long id) {
        //设置主键值
        enterpriseIdentification.setIdentificationId(id);
        //调用EnterpriseIdentificationService实现修改EnterpriseIdentification
        enterpriseIdentificationService.update(enterpriseIdentification);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增EnterpriseIdentification数据
     * @param enterpriseIdentification
     * @return
     */
    @PostMapping
    public Result add(@RequestBody EnterpriseIdentification enterpriseIdentification) {
        //调用EnterpriseIdentificationService实现添加EnterpriseIdentification
        enterpriseIdentificationService.add(enterpriseIdentification);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询EnterpriseIdentification数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<EnterpriseIdentification> findById(@PathVariable Long id) {
        //调用EnterpriseIdentificationService实现根据主键查询EnterpriseIdentification
        EnterpriseIdentification enterpriseIdentification = enterpriseIdentificationService.findById(id);
        return new Result<EnterpriseIdentification>(true, StatusCode.OK, "查询成功", enterpriseIdentification);
    }

    /***
     * 查询EnterpriseIdentification全部数据
     * @return
     */
    @GetMapping
    public Result<List<EnterpriseIdentification>> findAll() {
        //调用EnterpriseIdentificationService实现查询所有EnterpriseIdentification
        List<EnterpriseIdentification> list = enterpriseIdentificationService.findAll();
        return new Result<List<EnterpriseIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 修改公司认证信息
     * 相同代码写三次我也很绝望
     *
     * @param enterpriseIdentification
     * @return
     */
    @Transactional
    @PostMapping("authentication")
    public Result<EnterpriseIdentification> authentication(@RequestBody EnterpriseIdentification enterpriseIdentification) {
        String userNum = tokenDecode.getUserInfo().get("id");
        User user = userService.findUserByNum(userNum);
        String photo = null;
        if (user.getUserIdentiId() != 0) {
            EnterpriseIdentification old = new EnterpriseIdentification();
            old.setValidStatus(-2);
            old.setIdentificationId(user.getUserIdentiId());
            old = enterpriseIdentificationService.updateSelective(old);
            photo = old.getProfilePhoto();
        }

        enterpriseIdentification.setEnterpriseNumber(userNum);
        enterpriseIdentification.setSubmitTime(new Date());
        enterpriseIdentification.setSubmitterNumber(userNum);
        enterpriseIdentification.setProfilePhoto(photo);

        enterpriseIdentification = enterpriseIdentificationService.add(enterpriseIdentification);

        user.setUserIdentiId(enterpriseIdentification.getIdentificationId());
        user.setUserType(2);
        userService.update(user);

        return new Result<EnterpriseIdentification>(true, StatusCode.OK, "提交成功", enterpriseIdentification);
    }
}
